package com.xuyiwei.mayi.lock;

import com.xuyiwei.mayi.order.OrderNumGenerator;

/**
 * Created by xuyiwei on 2019/4/23.
 */
//使用多线程模拟生成订单号
public class OrderZkService implements Runnable {
    private OrderNumGenerator orderNumGenerator = new OrderNumGenerator();
    // 使用lock锁
    // private java.util.concurrent.locks.Lock lock = new ReentrantLock();
    private Lock lock = new ZookeeperDistrbuteLock();

    public void run() {
        try{
            lock.getLock();
            getNumber();

        }catch (Exception e){

        }finally {
            lock.unLock();
        }

    }

    public void getNumber() {
        String number = orderNumGenerator.getNumber();
        System.out.println(Thread.currentThread().getName() + ",生成订单ID:" + number);
    }

    public static void main(String[] args) {
        System.out.println("####生成唯一订单号###");
        for (int i = 0; i < 100; i++) {
            new Thread(new OrderZkService()).start();
        }

    }
}